package com.visionork.android.twe.parser;

import java.net.URL;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.visionork.android.twe.activity.Show;

public class IdolDramaAdapterImpl implements ShowAdapter {
	private static final String SHOW_URL_2011 = "http://sugoideas.com/search/2011drama";
	private static final String SHOW_URL_2010 = "http://sugoideas.com/search/2010drama";
	private static final String SHOW_URL_2009 = "http://sugoideas.com/search/2009drama";
	private static final String SHOW_URL_2008 = "http://sugoideas.com/search/2008drama";
	private static final String SHOW_URL_2007 = "http://sugoideas.com/search/2007drama";
	private static final String SHOW_URL_2006 = "http://sugoideas.com/search/2006drama";
	private static final String SHOW_URL_2005 = "http://sugoideas.com/search/2005drama";
	private static final String SHOW_URL_2004 = "http://sugoideas.com/search/2004drama";
	private static final String SHOW_URL_2003 = "http://sugoideas.com/search/2003drama";
	private static final String SHOW_URL_2002 = "http://sugoideas.com/search/2002drama";
	private static final String SHOW_URL_2001 = "http://sugoideas.com/search/2001drama";

	@Override
	public ArrayList<Show> get_show_list() throws Throwable {
		ArrayList<Show> show_list = new ArrayList<Show>();
		show_list.addAll(get_show_list(SHOW_URL_2011));
		show_list.addAll(get_show_list(SHOW_URL_2010));
		show_list.addAll(get_show_list(SHOW_URL_2009));
		show_list.addAll(get_show_list(SHOW_URL_2008));
		show_list.addAll(get_show_list(SHOW_URL_2007));
		show_list.addAll(get_show_list(SHOW_URL_2006));
		show_list.addAll(get_show_list(SHOW_URL_2005));
		show_list.addAll(get_show_list(SHOW_URL_2004));
		show_list.addAll(get_show_list(SHOW_URL_2003));
		show_list.addAll(get_show_list(SHOW_URL_2002));
		show_list.addAll(get_show_list(SHOW_URL_2001));
		return show_list;
	}

	private ArrayList<Show> get_show_list(String url_string) throws Throwable {
		ArrayList<Show> show_list = new ArrayList<Show>();
		URL url = new URL(url_string);
		Document doc = Jsoup.parse(url, 5000);
		Elements elms = doc.getElementsByTag("a");
		for (Element elm : elms) {
			Elements imgElms = elm.getElementsByTag("img");
			if (imgElms != null && imgElms.size() == 1) {
				if (imgElms.get(0).hasAttr("title") && elm.hasAttr("href")) {
					String title = imgElms.get(0).attr("title");
					String href = elm.attr("href");
					if (title.length() > 0) {
						show_list.add(new Show(title, href));
					}
				}
			}

		}
		return show_list;
	}
}
